package sk.tuke.serviceDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;

import sk.tuke.persistance.DiagnosePo;
import sk.tuke.persistance.DiseasePo;
import sk.tuke.persistance.DiseaseSymptomPo;
import sk.tuke.persistance.DrugPo;
import sk.tuke.persistance.SymptomPo;

public class DiagnoseManagementDao extends AbstractDao {

	public DiseasePo createDisease(String name, String description, java.util.Set<DiseaseSymptomPo> diseaseSymptom) {
		Session session = getSession();
		session.beginTransaction();
		DiseasePo disease = new DiseasePo();
		disease.setName(name);
		disease.setDescription(description);
		session.save(disease);
		session.getTransaction().commit();
		session.close();
		return disease;
	}

	public DiseasePo getDisease(String name) {
		return findEntityByName(DiseasePo.class, name);
	}

	public DiseasePo updateDisease(String existing, String name, String description, java.util.Set<DiseaseSymptomPo> diseaseSymptom) {
		Session session = getSession();
		DiseasePo disease = (DiseasePo) getDisease(existing);
		disease.setName(name);
		disease.setDescription(description);
		session.save(disease);
		session.getTransaction().commit();
		session.close();
		return disease;
	}

	public void deleteDisease(String existing) {
		Session session = getSession();
		DiseasePo disease = (DiseasePo) getDisease(existing);
		session.delete(disease);
	}

	public DrugPo createDrug(String name, String description) {
		Session session = getSession();
		session.beginTransaction();
		DrugPo drug = new DrugPo();
		drug.setName(name);
		drug.setDescription(description);
		session.save(drug);
		session.getTransaction().commit();
		session.close();
		return drug;
	}
	
	public DrugPo getDrug(String name) {
		return findEntityByName(DrugPo.class, name);
	}

	public List<DrugPo> getDrugs(String name) {
		return findEntityByNameWildCard(DrugPo.class, name);
	}
	
	public List<SymptomPo> getSymptoms(String name) {
		return findEntityByNameWildCard(SymptomPo.class, name);
	}

	public DrugPo updateDrug(String existing, String name, String description) {
		Session session = getSession();

		DrugPo drug = (DrugPo) getDrug(existing);
		drug.setName(name);
		drug.setDescription(description);

		session.save(drug);
		session.getTransaction().commit();
		session.close();
		return drug;
	}

	public void deleteDrug(String existing) {
		Session session = getSession();

		DrugPo drug = (DrugPo) getDrug(existing);
		session.delete(drug);
	}

	public DiagnosePo createDiagnose(Integer number, String name, String description, Set<SymptomPo> symptomPos, DiseasePo diseasePo, Set<DrugPo> drugPos) {
		Session session = getSession();
		session.beginTransaction();
		DiagnosePo diagnose = new DiagnosePo();
		diagnose.setName(name);
		diagnose.setNumber(number);
		diagnose.setDescription(description);
		diagnose.setDisease(diseasePo);
		diagnose.setDrugs(drugPos);
		diagnose.setSymptoms(symptomPos);
		session.save(diagnose);
		session.getTransaction().commit();
		session.close();
		return diagnose;
	}

	public DiagnosePo getDiagnose(Integer number) {
		return findEntityByNumber(DiagnosePo.class, number);
	}

	public void deleteDiagnose(Integer existing) {
		Session session = getSession();
		DiagnosePo diagnose = (DiagnosePo) getDiagnose(existing);
		session.delete(diagnose);
	}

	@SuppressWarnings("unchecked")
	public List<DiseaseSymptomPo> getDiseaseSymptom(Integer number) {
		Session session = getSession();
		Query query;
		List<DiseaseSymptomPo> diseaseSymptomPos = new ArrayList<DiseaseSymptomPo>();
		if (number == null) {
			query = session.createQuery("select u from DiseaseSymptomPo u");
		} else {
			query = session.createQuery("select u from DiseaseSymptomPo u where number =  :number");
			query.setString("number", number.toString());
		}
		diseaseSymptomPos.addAll(query.list());
		return diseaseSymptomPos;
	}

	public void deleteDiseaseSymptom(Integer existing) {
		Session session = getSession();

		DiseaseSymptomPo diseaseSymptom = (DiseaseSymptomPo) getDiseaseSymptom(existing);
		session.delete(diseaseSymptom);
	}

	public DiseasePo createDisease(String name, String description) {
		Session session = getSession();
		session.beginTransaction();
		DiseasePo disease = new DiseasePo();

		disease.setName(name);
		disease.setDescription(description);

		session.save(disease);
		session.getTransaction().commit();
		session.close();
		return disease;
	}

	@SuppressWarnings("unchecked")
	public List<DiseasePo> getDiseases(Integer number) {
		Session session = getSession();
		Query query;
		List<DiseasePo> diseasePos = new ArrayList<DiseasePo>();
		if (number == null) {
			query = session.createQuery("select u from DiseasePo u");
		} else {
			query = session.createQuery("select u from DiseasePo u where number =  :number");
			query.setString("number", number.toString());
		}
		diseasePos.addAll(query.list());
		return diseasePos;

	}

	@SuppressWarnings("unchecked")
	public DiseasePo updateDisease(Integer number, String name, String description) {
		Session session = getSession();
		Query query;
		List<DiseasePo> diseasePos = new ArrayList<DiseasePo>();
		if (number == null) {
			query = session.createQuery("select u from DiseasePo u");
		} else {
			query = session.createQuery("select u from DiseasePo u where number =  :number");
			query.setString("number", number.toString());
		}
		diseasePos.addAll(query.list());
		DiseasePo diseasePo = (DiseasePo) diseasePos;
		diseasePo.setName(name);
		diseasePo.setDescription(description);

		return diseasePo;

	}

}
